package com.imc.platform.base.user.service.impl;

import com.imc.platform.base.user.dao.repository.UserRepository;
import com.imc.platform.base.user.pojo.User;
import com.imc.platform.base.user.security.SSOAuthenticationManager;
import com.imc.platform.base.user.service.ISSOService;
import com.imc.platform.base.wxOauth2Token.dao.IOauthMapper;
import com.twp.common.tuple.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by tanweiping on 2018/9/19.
 */
@Service
@Slf4j
@Transactional
public class SSOServiceImpl implements ISSOService {

    @Autowired
    IOauthMapper oauthMapper;

    @Autowired
    private SSOAuthenticationManager authenticationManager;

    @Transactional
    public void SSOLogin(String loginName) throws AuthenticationException{
        Authentication request = new UsernamePasswordAuthenticationToken(loginName, "");
        log.info("login start");
        Authentication result = authenticationManager.authenticate(request);
        SecurityContextHolder.getContext().setAuthentication(result);
       log.info("log success");
    }

    @Transactional
    @Override
    public boolean addWXLogin(User user) {
        return oauthMapper.addWXLogin(user)>0;
    }
}
